package com.hyfrogx.modules.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyfrogx.modules.manage.dao.role.permission.RolePermissionDao;
import com.hyfrogx.modules.manage.entity.role.permission.RolePermission;
import com.hyfrogx.modules.manage.service.RolePermissionService;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 *
 * @项目 SpringMvc
 *
 * @日期: 2017年3月2日
 *
 * @负责人: LGQ
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionDao, RolePermission> implements RolePermissionService {

	Map<Integer, SimpleAuthorizationInfo> rolePermissionMap = new ConcurrentHashMap<Integer, SimpleAuthorizationInfo>();

	@Override
	public SimpleAuthorizationInfo findRolePermission(Integer rId) {
		return rolePermissionMap.get(rId);
	}

	@Override
	public void init() {
		List<RolePermission> ps = this.baseMapper.selectList(new QueryWrapper<>());
		Map<Integer, SimpleAuthorizationInfo> rpMap = new HashMap<Integer, SimpleAuthorizationInfo>();
		for (RolePermission p : ps) {
			SimpleAuthorizationInfo info = rpMap.get(p.getRoleId());
			if (info == null) {
				info = new SimpleAuthorizationInfo();
				rpMap.put(p.getRoleId(), info);
			}
			//info.addStringPermission(p.getPermission().getSn());
		}
		rolePermissionMap = rpMap;
	}


	/**
	 * 更新某个角色的缓存
	 * @param rid
	 */
	public void updateCacheMap(Integer rid) {
		List<RolePermission> ps = this.baseMapper.selectList(new QueryWrapper<RolePermission>().eq("roleId",rid));
		SimpleAuthorizationInfo info =  new SimpleAuthorizationInfo();
		for (RolePermission p : ps) {
			//info.addStringPermission(p.getPermission().getSn());
		}
		rolePermissionMap.put(rid,info);
	}

	/**
	 * 角色更新时批量插入
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void insertAll(Integer roleId, Integer[] permissionIds) {
//		if (roleId == null) {
//			throw new ServiceException("角色ID错误");
//		}
//		try {
//			//删除此角色的所有权限
//			rolePermissionMapper.deleteByRoleId(roleId);
//			if (permissionIds == null || permissionIds.length == 0) {
//				rolePermissionMap.remove(roleId);
//			}
//			rolePermissionMapper.insertAll(roleId, permissionIds);
//			//更新map缓存
//			updateCacheMap(roleId);
//		} catch (Exception e) {
//			throw new ServiceException("更新角色权限失败");
//		}
	}

	@Override
	public List<RolePermission> findByRoleId(Integer roleId){
		return this.baseMapper.selectList(new QueryWrapper<RolePermission>().eq("roleId",roleId));
	}
}
